﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Xml.Linq;

namespace SilverlightProject
{
    public partial class MainPage : Page
    {
        public MainPage()
        {
            InitializeComponent();

            InitData();
        }

        private void btnSize_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
        }

        private void InitData()
        {
            XElement root = XElement.Load("SiteMap.xml");

            var result = LoadData(root);

            treeView.DataContext = result;
        }

        private List<TreeViewModel> LoadData(XElement root)
        {
            if (root == null)
                return null;

            var items = from n in root.Elements("node")
                        select new TreeViewModel
                        {
                            Title = (string)n.Attribute("name"),
                            Address = new Uri((string)n.Attribute("address"), UriKind.Relative),
                            Children = LoadData(n)
                        };

            return items.ToList();
        }

        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (((TreeViewModel)e.NewValue).Address.ToString() != "")
                frame.Navigate(((TreeViewModel)e.NewValue).Address);
        }


        public class TreeViewModel
        {
            public string Title { get; set; }
            public Uri Address { get; set; }
            public List<TreeViewModel> Children { get; set; }
        }
    }
}
